let path = require("path");
let HtmlWebpackPlugin = require("html-webpack-plugin");
let MiniCssExtractPlugin = require("mini-css-extract-plugin");
let webpack = require("webpack");

module.exports = {
  entry: "./src/index.js", // 入口
  output: {
    filename: "index.js",
    path: path.resolve(__dirname, "dist"), // 路径必须是一个绝对路径
  },
  // 数组 放着所有的webpack插件
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html", // 模板
      filename: "index.html", // 文件名
      minify: {
        removeAttributeQuotes: true, // 删除index.html中的双引号
        collapseWhitespace: true, // 压缩成一行
      },
      // hash: true, // index.html引入脚本添加hash参数
    }),
    new webpack.DefinePlugin({
      // ENV: "'development'" 
      ENV: JSON.stringify('development'),
      FLAG: 'true',
      ADD: "1+1"
    }),
    new MiniCssExtractPlugin({
      filename: "css/main.css",
    }),
  ],
  // 模块
  module: {
    // 规则
    rules: [
      {
        test: /\.js$/, // normal 普通的loader
        use: {
          // babel-loader 将ES6转ES5
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
            plugins: ["@babel/plugin-transform-runtime"],
          },
        },
        include: path.resolve(__dirname, "src"),
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"],
      },
    ],
  },
};
